package com.neusoft.yuejian_01.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.base.BaseActivity;
import com.neusoft.yuejian_01.javabean.AttentionBean;
import com.neusoft.yuejian_01.javabean.IsAttentionBean;
import com.neusoft.yuejian_01.javabean.ProfessorBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.AttentionModel;
import com.neusoft.yuejian_01.model.Const;
import com.neusoft.yuejian_01.model.ConstPhoto;
import com.neusoft.yuejian_01.model.ProfessorModel;

import it.sephiroth.android.library.picasso.Picasso;

public class ProfessorDetailActivity extends BaseActivity implements RetrofitListener {
    private TextView clubView;
    private TextView professorView,professornameView;
    private TextView clubnameView;
    private TextView professorintroduceView;
    protected Context context;
    private Button courseButton;//大师课程按钮
    private ImageButton button_back,professorButton;//返回按钮、关注按钮
    public ImageView professorImageView;
    private int pm_id;
    private String pm_name;
    private boolean flags;

     protected void initView() {
         setContentView(R.layout.activity_professor_detail);
        clubView = (TextView) findViewById(R.id.title_professor);
        professorView = (TextView) findViewById(R.id.professor_name);
        professornameView = (TextView) findViewById(R.id.professorname);
        clubnameView = (TextView) findViewById(R.id.club_professor);
        professorintroduceView = (TextView) findViewById(R.id.professor_introduce);
        professorImageView =(ImageView) findViewById(R.id.professor_image);
        button_back = (ImageButton) findViewById(R.id.button_back);
        courseButton = (Button) findViewById(R.id.course_button);
        professorButton = (ImageButton) findViewById(R.id.professorButton);
    }
     protected void initEvents() {
        professorButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AttentionModel attentionModel = new AttentionModel();
                attentionModel.userCollectProfessor(String.valueOf(getUser_id()),String.valueOf(pm_id),ProfessorDetailActivity.this);
            }
        });
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(ProfessorDetailActivity.this,ProfessorActivity.class);
                startActivityForResult(intent,1);
            }
        });
        courseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(ProfessorDetailActivity.this,ProfessorCourseActivity.class);
                intent.putExtra("pm_id",pm_id);
                intent.putExtra("pm_name",pm_name);
                startActivityForResult(intent,1);
            }
        });
    }
     protected void initData() {
        pm_id = getIntent().getIntExtra("pm_id", -1);
        pm_name = getIntent().getStringExtra("pm_name");
        final ProfessorModel professorModel = new ProfessorModel();
        professorModel.Pmdetail(String.valueOf(pm_id), this);
        AttentionModel attentionModel = new AttentionModel();
        attentionModel.isCollected(String.valueOf(getUser_id()), String.valueOf(pm_id), "3", this);//flag=3.判断大师关注状态
    }
    @Override
    public void onSuccess(Object object,int flag) {
        switch (flag){
            case Const.ProfessorDetail:
            //flag标识为1，获取大师详情
            ProfessorBean Pmdetail =  (ProfessorBean)object;
            clubView.setText(Pmdetail.getC_name());
            professorView.setText(Pmdetail.getPm_name());
            professornameView.setText(Pmdetail.getPm_name());
            clubnameView.setText(Pmdetail.getC_name());
            professorintroduceView.setText("\u3000\u3000"+Pmdetail.getPm_info());
            clubnameView.setText(Pmdetail.getC_name());
            final String professorPic = ConstPhoto.BASE_URL+ConstPhoto.ProfessorFile + Pmdetail.getPm_pic();
            Picasso.with(context)
                    .load(professorPic)
                    .placeholder(R.drawable.pic2)
                    .into(professorImageView);
                break;
            case Const.IsAttention:
                //flag标识为2，判断关注状态
                IsAttentionBean isAttentionBean=(IsAttentionBean)object;
                if (isAttentionBean.getCollected().equals("1")){
                    //已关注
                    flags=true;
                    professorButton.setImageResource(R.drawable.ic_attention_checked);
                }
                else {
                    //未关注
                    flags=false;
                    professorButton.setImageResource(R.drawable.ic_attention);
                }
                break;
            case Const.ProfessorAttention:
                AttentionBean attentionBean=(AttentionBean)object;
                if (attentionBean.getSuccess().equals("1")){
                    Log.e(attentionBean.getSuccess(),"---------");
                    if(flags){
                        flags=false;
                        professorButton.setImageResource(R.drawable.ic_attention);
                    }else {
                        flags=true;
                        professorButton.setImageResource(R.drawable.ic_attention_checked);
                    }
                    Toast.makeText(ProfessorDetailActivity.this, "OK", Toast.LENGTH_SHORT).show();
                }
                break;
     }
    }
    @Override
    public void onFail() {
        Toast.makeText(this,"fail",Toast.LENGTH_LONG).show();

    }
}
